<?php


namespace app\api\controller;


use app\BaseController;
use app\middleware\Auth;
use think\middleware\AllowCrossDomain;

class Upload extends BaseController
{
    protected $middleware = [AllowCrossDomain::class,Auth::class];
    public function index(){

    }

    public function upload_image()
    {
        $config = config('filesystem');
        $root = $config['disks'][$config['default']]['root'];
        $url = $config['disks'][$config['default']]['url'];

        $file = $this->request->file('file');
        //20220301\cbb586f14fe5b5c8a064d333ba625bc1.jpg
        try {
            validate(['file' => [
                'fileSize' => 1024 * 1024 * 10,//10MB
                'fileExt' => 'jpg,png,gif',
            ]])->check(['file' => $file]);
            $savename = \think\facade\Filesystem::putFile('', $file);
            return $this->msg(1, '', '', [
                'url' => $url . str_replace("\\", "/", $savename),
            ]);
        } catch (\think\exception\ValidateException $e) {
            return $this->msg(0, $e->getMessage());
        }

    }

    public function upload_file()
    {
        $config = config('filesystem');
        $root = $config['disks'][$config['default']]['root'];
        $url = $config['disks'][$config['default']]['url'];

        $file = $this->request->file('file');
        //20220301\cbb586f14fe5b5c8a064d333ba625bc1.jpg
        try {
            validate(['file' => [
                'fileSize' => 1024 * 1024 * 10,//10MB
                'fileExt' => 'jpg,png,gif,rar,zip,pdf,txt,doc,docx,ppt,pptx,xls,xlsx,swf',
            ]])->check(['file' => $file]);
            $savename = \think\facade\Filesystem::putFile('', $file);
            return $this->msg(1, '', '', [
                'url' => $url . str_replace("\\", "/", $savename),
            ]);
        } catch (\think\exception\ValidateException $e) {
            return $this->msg(0, $e->getMessage());
        }

    }

    public function tinymce_upload_image()
    {
        $config = config('filesystem');
        $root = $config['disks'][$config['default']]['root'];
        $url = $config['disks'][$config['default']]['url'];

        $file = $this->request->file('file');
        //20220301\cbb586f14fe5b5c8a064d333ba625bc1.jpg
        try {
            validate(['file' => [
                'fileSize' => 1024 * 1024 * 10,//10MB
                'fileExt' => 'jpg,png,gif',
            ]])->check(['file' => $file]);
            $savename = \think\facade\Filesystem::putFile('', $file);
            return $this->msg(1, '', '', [
                'url' => $url . str_replace("\\", "/", $savename),
                'filename' => basename($savename),
            ]);
        } catch (\think\exception\ValidateException $e) {
            return $this->msg(0, $e->getMessage());
        }
    }

    public function tinymce_upload_file()
    {
        $config = config('filesystem');
        $root = $config['disks'][$config['default']]['root'];
        $url = $config['disks'][$config['default']]['url'];

        $file = $this->request->file('file');
        //20220301\cbb586f14fe5b5c8a064d333ba625bc1.jpg

        try {
            validate(['file' => [
                'fileSize' => 1024 * 1024 * 10,//10MB
                'fileExt' => 'jpg,png,gif,rar,zip,pdf,txt,doc,docx,ppt,pptx,xls,xlsx,swf',
            ]])->check(['file' => $file]);
            $savename = \think\facade\Filesystem::putFile('', $file);
            return $this->msg(1, '', '', [
                'url' => $url . str_replace("\\", "/", $savename),
                'filename' => basename($savename),
            ]);
        } catch (\think\exception\ValidateException $e) {
            return $this->msg(0, $e->getMessage());
        }
    }
}